package com.pri.chat.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.FriendsListBean;
import com.netease.nim.uikit.net.entity.TiXianNote;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.activity.OtherPeopleMsgActivity;
import com.pri.chat.adapter.FriendsAdapter;
import com.pri.chat.adapter.TiXianNoteListAdapter;
import com.pri.chat.model.GzModel;
import com.pri.chat.model.UserModel;
import com.pri.chat.utils.Des3Util;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.fragment.FragmentLazy;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/24
 */
public class TiXianFragment extends FragmentLazy {

    Unbinder unbinder;
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    private int type;
    private int page = 1;
    private boolean isShowDialog = false;

    public static TiXianFragment getInstance() {
        return new TiXianFragment();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        unbinder.unbind();
    }

    @Override
    protected View initViews(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = layoutInflater.inflate(R.layout.fragment_tixian_list_layout, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        initLayout();
        initSrl();
        return view;
    }
        private List<TiXianNote> mlist = new ArrayList<>();
        private TiXianNoteListAdapter mAdapter;
        private void initLayout() {
            LinearLayoutManager linearLayoutManager =new LinearLayoutManager(mContext);
            recMain.setLayoutManager(linearLayoutManager);
            recMain.setNestedScrollingEnabled(false);
            mAdapter=new TiXianNoteListAdapter(R.layout.item_recharge_list,mlist);
            recMain.setAdapter(mAdapter);
        }

    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<TiXianNote>>> onNextListener = baseBean -> {
            mlist.addAll(baseBean.getData());
            mAdapter.notifyDataSetChanged();
        };
        UserModel regModel = new UserModel();//登录上传参数
        regModel.setMemberId(SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(regModel));
        HttpMethods.getInstance().listTiXian(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            isShowDialog = false;
            page++;
            initData();
        });
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mlist.clear();
        mAdapter.notifyDataSetChanged();
        initData();
    }
}
